package com.guoyansoft.controller;

import com.guoyansoft.config.R;
import com.guoyansoft.model.dao.AddressDao;
import com.guoyansoft.model.pojo.Address;
import com.guoyansoft.model.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 常利军
 * 地址业务控制器
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     *
     * @param district 根据区多条件查询
     * @param curPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryByPage")
    public R queryByPage(String district,Integer curPage,Integer pageSize){
        if(district==null){
            district="";
        }
        Map<String,Object> params=new HashMap<>();
        params.put("districtKey","%"+district+"%");
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        R r = addressService.queryByPage(params);
        return r;
    }

    /**
     * 新增地址实体信息
     * @param address
     * @return
     */
    @RequestMapping("/save")
    public R save(Address address,Integer customerId){
        R save = addressService.save(address,customerId);
        return save;
    }

    /**
     * 根据id删除地址信息
     * @param id
     * @return
     */
    @RequestMapping("/drop")
    public R drop(Integer[] id){
        R drop = addressService.drop(id);
        return drop;
    }

    /**
     * 根据id修改地址实体信息
     * @param address
     * @return
     */
    @RequestMapping("/update")
    public R update(Address address){
        R modify = addressService.modify(address);
        return modify;
    }

    /**
     * 根据用户id查地址信息
     * @param customerId 用户id
     * @return
     */
    @RequestMapping("/queryByCustomerId")
    public R queryByCustomerId(Integer customerId){
        R queryByCustomerId = addressService.queryByCustomerId(customerId);
        return queryByCustomerId;
    }

    /**
     * 根据id查地址实体
     * @param id
     * @return
     */
    @RequestMapping("/queryById")
    public R queryById(Integer id){
        R r = addressService.queryById(id);
        return r;
    }
    /**
     * 查询所有不重复的区域
     * @param
     * @return
     */
    @RequestMapping("/queryDistrict")
    public R queryDistrict(){
        R r = addressService.queryDistrict();
        return r;
    }
    @RequestMapping("/queryAll")
    public R queryAll(){
        R r = addressService.queryAll();
        return r;
    }
}
